import Core.WindFunctions as WindFunction
import DataProcess
import datetime
import Core.Config as Config
import Core.WindFunctions as Wind
import Core.Gadget as Gadget
import os
import DataProcess.InstrumentList
import DataProcess.General

# ---冷启动，建立指数库---
def Download_IndexInfo(database ,datetime1, symbols):
    symbols = []
    # ---Stock---
    # symbols.append("000300.SH")
    # symbols.append("000016.SH")
    # symbols.append("000001.SH")#上证
    # symbols.append("399106.SZ")#深圳综合
    # symbols.append("399001.SZ")#深圳成分
    # symbols.append("399006.SZ")#创业板指数
    # symbols.append("399005.SZ")#中小板
    # symbols.append("000903.SH")#100
    # symbols.append("000904.SH")#200
    # symbols.append("000905.SH")#500
    # symbols.append("000906.SH")#800
    # symbols.append("000852.SH")#1000

    # ---Fix Income---
    # symbols.append("H11001.CSI")
    # ---国债与金融债---
    # symbols.append("H11006.CSI")
    # symbols.append("H11007.CSI")
    # symbols.append("H11017.CSI")
    # symbols.append("H11075.CSI")
    # symbols.append("H11099.CSI")
    # ---信用债---
    # symbols.append("000923.CSI")
    # symbols.append("H11008.CSI")
    # ---海外---
    # symbols.append("HSCI.HI")
    # symbols.append("DJI.GI")
    # symbols.append("IXIC.GI")
    # symbols.append
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


def Download_SWIndustryIndex(database ,datetime1, symbols):
    symbols = []
    # data, dt = WSet_Instruments(datetime1, sectorName="申银万国一级行业指数")
    data, dt = WindFunction.WSet_Instruments(datetime1, sectorName="中信证券一级行业指数")
    for d in data:
        symbols.append(d["Symbol"])
    #
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


def Download_CSIndustryIndex(database ,datetime1, symbols):
    symbols = []
    # data, dt = WSet_Instruments(datetime1, sectorName="申银万国一级行业指数")
    data, dt = WindFunction.WSet_Instruments(datetime1, sectorName="中信证券一级行业指数")
    for d in data:
        symbols.append(d["Symbol"])
    #
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


#---下載指数成分股---
def Download_InstrumentList_Content(database, datetime1, datetime2):
    # ---Get Sector Instrument List from Wind---
    # datetime1 = datetime(2016,1,1)
    # Request_Write_InstrumentList(database, datetime1, "000016.SH")
    # Wind.Request_Write_InstrumentList(database,datetime1, "000300.SH")
    # Request_Write_InstrumentList(database,datetime1, "000905.SH")
    # Request_Write_InstrumentList(database,datetime1, "000906.SH")


    # ---更新不同日期的指数列表---
    # ,hs300 一般为6月初和12月实施调整
    datetime0 = datetime1
    while datetime0 <= datetime2:
        datetime0 = datetime0 + datetime.timedelta(days=1)
        #for month in range(1, 13, 6):
        if (datetime0.month == 1 and datetime0.day == 1)  or (datetime0.month == 7 and datetime0.day==1):
            print(str(datetime0))
            DataProcess.Request_Write_InstrumentList(database, datetime0, "000016.SH")
            DataProcess.Request_Write_InstrumentList(database, datetime0, "000300.SH")
            DataProcess.Request_Write_InstrumentList(database, datetime0, "000905.SH")
            DataProcess.Request_Write_InstrumentList(database, datetime0, "000906.SH")
    kkwood = 0


def Download_NewIndex_20200312(database):
    #
    datetime1 = datetime.datetime(2020,3,12)

    symbols = []
    # ---Stock---
    symbols.append("881001.WI") # 万得全A
    symbols.append("881008.WI") # 万得全A 除科创班
    symbols.append("881003.WI") # 万得全A 除石油石化
    #
    symbols.append("CI005917.WI") # 中信金融
    symbols.append("CI005918.WI") # 中信周期
    symbols.append("CI005919.WI") # 中信消费
    symbols.append("CI005920.WI") # 中信成长
    symbols.append("CI005921.WI") # 中信稳定
    #
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


def Download_NewIndex_20201103(database):
    #
    datetime1 = datetime.datetime(2020,3,12)

    symbols = ["H00986.CSI", "H00987.CSI", "H00988.CSI", "H00989.CSI", "H00990.CSI", "H00991.CSI", "H00992.CSI", "H00993.CSI"]
    #
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


def Download_NewIndex_20200326(database):
    #
    datetime1 = datetime.datetime(2020, 3, 26)

    symbols = []
    #
    symbols.append("801060.SI") # 建筑建材
    symbols.append("801100.SI") # 信息设备
    symbols.append("801090.SI") # 交运设备
    symbols.append("801220.SI") # 信息服务
    symbols.append("801990.SI") # 金融服务
    #
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


def Download_NewIndex_20200821(database):
    #
    datetime1 = datetime.datetime(2020, 8, 21)

    symbols = []
    #
    symbols.append("CI005901.WI") # 上游 中信
    symbols.append("CI005902.WI") # 中游 中信
    symbols.append("CI005903.WI") # 下游 中信
    #
    symbols.append("CI005904.WI") # 基础设施及建设（组合，中信）
    symbols.append("CI005905.WI") # TMT（组合，中信）
    symbols.append("CI005909.WI") # 金融地产（组合，中信）
    symbols.append("CI005910.WI") # 资源能源（组合，中信）
    symbols.append("CI005911.WI") # 原材料（组合，中信）
    symbols.append("CI005912.WI") # 设备制造（组合，中信）
    symbols.append("CI005913.WI") # 工业服务（组合，中信）
    symbols.append("CI005914.WI") # 交通运输（组合，中信）
    symbols.append("CI005915.WI") # 消费 （组合，中信）
    symbols.append("CI005916.WI") # 科技（组合，中信）
    #
    symbols.append("399372.SZ")  # 大盘成长
    symbols.append("399373.SZ")  # 大盘价值
    symbols.append("399376.SZ")  # 小盘成长
    symbols.append("399377.SZ")  # 小盘价值
    #
    DataProcess.Request_Write_Index_List(database, datetime1, symbols)


def Download_NewIndex_20200921(database):
    #
    datetime2 = datetime.datetime(2020, 9, 21)

    symbols = []
    #
    symbols.append("CN6112.CNI") # 货基指数
    DataProcess.Request_Write_Index_List(database, datetime2, symbols)
    #
    for symbol in symbols:
        instrument = database.Find("Instruments", "Index", {"Symbol": symbol})
        instrument = instrument[0]
        datetime1 = instrument["datetime1"]
        DataProcess.Download_DailyBars(database, symbol, "index", datetime1, datetime2)


def Download_NewIndex_20210618(database):
    #
    datetime1 = datetime.datetime(2020, 3, 26)

    symbols = []
    #
    symbols.append("801811.SI")  #大盘指数
    symbols.append("801812.SI") # 中盘指数
    symbols.append("801813.SI") # 小盘指数

    symbols.append("801821.SI") #
    symbols.append("801822.SI") #
    symbols.append("801823.SI") #
    symbols.append("801831.SI") #
    symbols.append("801832.SI") #
    symbols.append("801833.SI") #

    #
    DataProcess.InstrumentList.Request_Write_Index_List(database, datetime1, symbols)


def Download_NewIndex_20210618(database):
    #
    update_datetime = datetime.datetime(2021, 6, 21)

    symbols = []
    #
    symbols.append("885012.WI")  # 股票型基金总指数
    symbols.append("885000.WI")  # 普通股票型基金
    symbols.append("885004.WI")  # 股票指数型基金指数(885004)
    symbols.append("885044.WI")  # 增强指数型基金指数(885044)

    symbols.append("885013.WI")  # 混合型基金总指数
    symbols.append("885001.WI")  # 偏股混合型基金
    symbols.append("885003.WI")  # 偏债混合型
    symbols.append("885002.WI")  # 平衡混合型基金指数(885002)
    symbols.append("885061.WI")  # 灵活配置型基金指数(885061)

    symbols.append("885005.WI")  # 债券型基金指数(885005)
    symbols.append("885006.WI")  # 混合债券型一级
    symbols.append("885007.WI")  # 混合债券型二级
    symbols.append("885008.WI")  # 中长期纯债
    symbols.append("885063.WI")  # 债券指数型基金指数(885063)

    symbols.append("885009.WI")  # 货币市场型
    symbols.append("885054.WI")  # QDII基金指数
    symbols.append("885064.WI")  # 另类投资基金指数(885064)
    symbols.append("885010.WI")  # 中国基金总指数
    #
    DataProcess.InstrumentList.Request_Write_Index_List(database, update_datetime, symbols)

    for symbol in symbols:
        instrument = database.Find("financial_data", "instrument_index", {"symbol": symbol})
        instrument = instrument[0]
        datetime1 = instrument["datetime1"]
        datetime2 = update_datetime
        DataProcess.General.Download_DailyBars(database, symbol, "mutualfund", datetime1, datetime2)


def Download_Bars_20200821(database):
    #
    symbols = []
    #
    symbols.append("000001.SH")
    #
    for symbol in symbols:
        instrument = database.Find("Instruments", "Index", {"Symbol": symbol})
        instrument = instrument[0]
        datetime1 = instrument["datetime1"]
        datetime2 = datetime.datetime(2016, 8, 21)
        DataProcess.General.Download_DailyBars(database, instrument, datetime1, datetime2)


if __name__ == '__main__':
    #
    # pathfilename = os.getcwd() + "\..\Config\config2.json"
    # config = Config.Config(pathfilename)
    # database = config.DataBase("JDMySQL")
    # realtime = config.RealTime()

    path_filename = os.getcwd() + "\..\Config\config_local.json"
    database = Config.create_database(database_type="MySQL", config_file=path_filename, config_field="MySQL")

    Wind.w.start()

    # ---定向更新指数---
    # Download_NewIndex_20200921(database)
    # Download_NewIndex_20201103(database)
    # Download_NewIndex_20200821(database)
    # Download_Bars_20200821(database)
    Download_NewIndex_20210618(database)

    # ---补齐数据---
    datetime1 = datetime.datetime(2005, 1, 1)
    datetime2 = datetime.datetime(2020, 11, 1)
    DataProcess.Download_DailyBars(database, "000300.SH", "Index", datetime1, datetime2)

    # 2020-7-27 补充指数字段 份额
    # instrument = database.Find("Instruments", "Index", {"Symbol": "000300.SH"})
    # instrument = instrument[0]
    # datetime1 = datetime.datetime(2000, 12, 31)
    # datetime2 = datetime.datetime(2016, 8, 21)
    # DataProcess.Download_DailyBars(database, instrument, datetime1, datetime2)

    # 冷启动
    # 2020-7-11,
    # 2020-8-21 再次进行冷启动
    datetime2 = datetime.datetime(2020, 10, 31)
    DataProcess.Automatic_Download_DailyQuote(database, datetime2, instrumentType="Index", startIndex=0,
                                              config=config)

    # ---统计指定日期的报告---
    datetime1 = datetime.datetime(2007, 1, 1)
    datetime2 = datetime.datetime(2019, 6, 30)
    # Download_InstrumentList_Content(database, datetime1, datetime2)

    # ---20200312 定向更新几个指数---
    Download_NewIndex_20200312(database)

    # ---定向更新SW 已经消失指数---
    # Download_NewIndex_20200326(database)
    # DataProcess.Automatic_Download_DailyQuote(database,
    #                                           datetime.datetime(2020, 3, 26),
    #                                           instrumentType="Index",
    #                                           startIndex=0,
    #                                           config=config)
